package com.ffcs.crmd.tsp.server.jdbc.repository;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobGroup;

/**
 * 
 * 功能说明:任务仓库层
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobGroupRepository extends AbstractJdbcRepository {
    
    
    public boolean execute(String sql, List<Object> params, Connection connection) throws TspException{
        return super.execute(sql, params, connection);
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param params
     * @return
     * @throws TspException
     */
    public int queryRows(String sql, Connection connection) throws TspException{
        return queryRows(sql, null, connection);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param params
     * @return
     * @throws TspException
     */
    public int queryRows(String sql, List<Object> params, Connection connection) throws TspException{
        return super.queryRows(sql, params, connection);
    }
    
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @return
     * @throws TspException
     */
    public List<JobGroup> queryList(String sql, Connection connection) throws TspException {
        return queryList(sql, null, connection);
    }
    
    /**
     * 
     * 功能说明:查询任务
     * 
     * @param sql 语句
     * @param params 参数
     * @return 任务数组
     * @throws TspException
     */
    public List<JobGroup> queryList(String sql, List<Object> params, Connection connection) throws TspException {
        List<JobGroup> jobGroupList = new ArrayList<JobGroup>();
        List<Map<String, Object>> list = super.queryBatch(sql, params, connection);
        for (Map<String, Object> ret : list) {
            JobGroup jobGroup = new JobGroup();
            Iterator<Entry<String, Object>> it = ret.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                String name = entry.getKey();
                Object value = entry.getValue();
                if (COL_ID.equalsIgnoreCase(name)) {
                    if(StringUtils.isNotBlank(String.valueOf(value))){
                        jobGroup.setId(Long.valueOf(String.valueOf(value)));
                    }
                } else if (COL_NAME.equalsIgnoreCase(name)) {
                    jobGroup.setName(String.valueOf(value));
                } else if (COL_DESCRIPTION.equalsIgnoreCase(name)) {
                    jobGroup.setDescription(String.valueOf(value));
                } else if (COL_CLUSTER_NAME.equalsIgnoreCase(name)) {
                    jobGroup.setClusterName(String.valueOf(value));
                } 
            }
            jobGroupList.add(jobGroup);
        }
        return jobGroupList;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @return
     * @throws TspException
     */
    public JobGroup query(String sql, Connection connection) throws TspException {
        return query(sql, null, connection);
    }
    
    
    /**
     * 
     * 功能说明:查询任务
     * 
     * @param sql 语句
     * @param params 参数
     * @return 查询结果
     * @throws TspException
     */
    public JobGroup query(String sql, List<Object> params, Connection connection) throws TspException {
        Map<String, Object> rmap = super.querySingle(sql, params, connection);
        if (MapUtils.isNotEmpty(rmap)) {
            JobGroup jobGroup = new JobGroup();
            Iterator<Entry<String, Object>> it = rmap.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                String name = entry.getKey();
                Object value = entry.getValue();
                if (COL_ID.equalsIgnoreCase(name)) {
                    if(StringUtils.isNotBlank(String.valueOf(value))){
                        jobGroup.setId(Long.valueOf(String.valueOf(value)));
                    }
                } else if (COL_NAME.equalsIgnoreCase(name)) {
                    jobGroup.setName(String.valueOf(value));
                } else if (COL_DESCRIPTION.equalsIgnoreCase(name)) {
                    jobGroup.setDescription(String.valueOf(value));
                } else if (COL_CLUSTER_NAME.equalsIgnoreCase(name)) {
                    jobGroup.setClusterName(String.valueOf(value));
                }  
            }
            return jobGroup;
        }
        return null;
    }
}
